﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using ThouShaltNotBrick.Models;
using GMUtilitiesDotNet.Threading;
using ThouShaltNotBrickLevelEditor.Code.Renderers;
using GMUtilitiesDotNet.Events;
using ThouShaltNotBrickLevelEditor.Code.GeneralObjects;

namespace ThouShaltNotBrickLevelEditor
{
    /// <summary>
    /// Interaction logic for LevelView.xaml
    /// </summary>
    public partial class LevelView : UserControl
    {
        #region Properties
        private CustomBackgroundWorker<LevelRendererParameters, LevelRendererReturnValue> mLevelRenderer;
        #endregion

        #region Dependency Properties
        #region BrickStatesTypesImages
        public static readonly DependencyProperty BrickStatesTypesImagesProperty =
            DependencyProperty.Register(
                "BrickStatesTypesImages",
                typeof(BrickStatesTypesImages),
                typeof(LevelView),
                new PropertyMetadata(null, OnBrickStatesTypesImagesChanged));

        public BrickStatesTypesImages BrickStatesTypesImages
        {
            get { return (BrickStatesTypesImages)GetValue(BrickStatesTypesImagesProperty); }
            set { SetValue(BrickStatesTypesImagesProperty, value); }
        }

        private static void OnBrickStatesTypesImagesChanged(DependencyObject pObj, DependencyPropertyChangedEventArgs pArgs)
        {
            ((LevelView)pObj).OnBrickStatesTypesImagesChanged(pArgs);
        }

        private void OnBrickStatesTypesImagesChanged(DependencyPropertyChangedEventArgs pArgs)
        {
            Redraw();
        }
        #endregion

        #region LevelData
        public static readonly DependencyProperty LevelDataProperty =
            DependencyProperty.Register(
                "LevelData",
                typeof(LevelData),
                typeof(LevelView),
                new PropertyMetadata(null, OnLevelDataChanged));

        public LevelData LevelData
        {
            get { return (LevelData)GetValue(LevelDataProperty); }
            set { SetValue(LevelDataProperty, value); }
        }

        private static void OnLevelDataChanged(DependencyObject pObj, DependencyPropertyChangedEventArgs pArgs)
        {
            ((LevelView)pObj).OnLevelDataChanged(pArgs);
        }

        private void OnLevelDataChanged(DependencyPropertyChangedEventArgs pArgs)
        {
            Redraw();
        }
        #endregion
        #endregion

        #region Constructors
        public LevelView()
        {
            InitializeComponent();

            mLevelRenderer = new CustomBackgroundWorker<LevelRendererParameters, LevelRendererReturnValue>(
                this,
                RendererFactory.CreateLevelRenderer());
        }
        #endregion

        #region Overriden Methods
        protected override void OnRenderSizeChanged(SizeChangedInfo sizeInfo)
        {
            Redraw();
            base.OnRenderSizeChanged(sizeInfo);
        }
        #endregion

        #region Methods
        public void Redraw()
        {
            LevelRendererParameters lrp = new LevelRendererParameters(
                ActualWidth,
                ActualHeight,
                BrickStatesTypesImages,
                LevelData);

            mLevelRenderer.Work(lrp);
        }
        #endregion

        #region Event Handlers
        private void LoadedHandler(object sender, RoutedEventArgs e)
        {
            mLevelRenderer.WorkComplete += LevelRendererWorkCompleteHandler;
            Redraw();
        }

        private void UnloadedHandler(object sender, RoutedEventArgs e)
        {
            mLevelRenderer.WorkComplete -= LevelRendererWorkCompleteHandler;
        }

        private void LevelRendererWorkCompleteHandler(object sender, ParameterizedEventArgs<LevelRendererReturnValue> e)
        {
            if (e.Parameter != null)
            {
                RenderTargetBitmap levelBitmap = e.Parameter.LevelBitmap;
                LevelImage.Source = levelBitmap;
            }
            else
            {
                LevelImage.Source = null;
            }
        }
        #endregion
    }
}